home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9824 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  46 lines

  1. Path: tech.cftnet.com!not-for-mail
  2. From: wcowley@cftnet.com (Wes Cowley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help needed: reference parameter
  5. Date: 4 Mar 1996 11:03:28 GMT
  6. Organization: CFTnet
  7. Message-ID: <4heim1$nqr@tech.cftnet.com>
  8. References: <4gv7al$o0c$1@mhafc.production.compuserve.com>
  9. NNTP-Posting-Host: ppp244_4.cftnet.com
  10. X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
  11.  
  12. Terry Nikkel (102101.2325@CompuServe.COM) wrote:
  13. : I am posting this for a colleague, thanks for your help:
  14. : I want to pass a reference (default 0) parameter to a function.  
  15. : For example, assume classes 'AClass' and 'BClass'; assume 
  16. : 'AClass' has a method called 'Function'.
  17. : //AClass.hxx
  18. : #include "BClass.hxx"
  19. : bool AClass::Function (BClass& = 0);  // would be the 
  20. : specification
  21. : //AClass.cxx
  22. : bool AClass::Function (BClass& refer)  // is the actual method
  23. : {
  24. :     if (! (refer == 0)  {
  25. :         ...
  26. :     {
  27. : }
  28.  
  29. The problem is that 0 is not a valid value for references.  For pointers, 
  30. 0 is used as the null pointer but it doesn't work that way for references.
  31. One technique that might be appropriate for you is to define a dummy 
  32. instance of BClass that is treated as a missing reference:
  33.  
  34. class BClass {
  35. ...
  36.    static BClass nullBClass;
  37. ... };
  38.  
  39. BClass BClass::nullBClass;
  40.  
  41. bool AClass::Function(BClass& = BClass::nullBClass);
  42.  
  43. Wes
  44.